package com.dzl.fee;

import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeUserCustomers;
import com.dzl.fee.pojo.WelfareFeeUserCustomersSalesRel;
import com.dzl.fee.pojo.query.WelfareFeeUserCustomersQuery;
import com.dzl.fee.pojo.vo.WelfareFeeUserCustomersVo;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.fee.service.WelfareFeeUserCustomersSalesRelService;
import com.dzl.fee.service.WelfareFeeUserCustomersService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 客户管理 - 控制层
 * @author WangJian
 * @date 2021/8/3 0003
 */
@Api(value = "企业端-客户管理", tags = "企业端-客户管理")
@RestController
@RequestMapping("/customers")
public class CustomersController extends BaseController {

    @Autowired
    private WelfareFeeUserCustomersService welfareFeeUserCustomersService;
    @Autowired
    private WelfareFeeOrderService welfareFeeOrderService;
    @Autowired
    private WelfareFeeUserCustomersSalesRelService welfareFeeUserCustomersSalesRelService;

    /**
     * 客户列表
     * @date 2021/8/3 0003 16:10
     * @author wangjian
     * @param query 客户列表查询条件
     * @return com.dzl.util.PageUtils<com.dzl.fee.customer.pojo.vo.WelfareFeeUserCustomersVo>
     */
    @ApiOperation(value = "企业端-客户列表", notes = "企业端-客户列表")
    @GetMapping("/getCustomersList")
    public PageUtils<WelfareFeeUserCustomersVo> getCustomersList(WelfareFeeUserCustomersQuery query){
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareFeeUserCustomersVo> pageInfo = welfareFeeUserCustomersService.getCustomersVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 保存客户
     * @date 2021/8/3 0003 17:15
     * @author wangjian
     * @param welfareFeeUserCustomers 客户信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-保存客户", notes = "企业端-保存客户")
    @PostMapping("/saveCustomer")
    public JsonResult<Integer> saveCustomer(@RequestBody WelfareFeeUserCustomers welfareFeeUserCustomers){
        if (EmptyUtils.isEmpty(welfareFeeUserCustomers.getName())){
            return JsonResult.failWithMsg("客户姓名不能为空");
        }
        if (EmptyUtils.isEmpty(welfareFeeUserCustomers.getId())){
            welfareFeeUserCustomers.setStatus(CommonConstants.YES);
            welfareFeeUserCustomers.setCreaterName(getAdminInfo().getUserName());
            welfareFeeUserCustomers.setEnterpriseId(getAdminInfo().getEnterpriseId());
        }else {
            welfareFeeUserCustomers.setUpdateTime(new Date());
            welfareFeeUserCustomers.setUpdaterName(getAdminInfo().getUserName());
        }
        return JsonResult.success(welfareFeeUserCustomersService.updateCustomers(welfareFeeUserCustomers));
    }

    /**
     * 删除客户
     * @date 2021/8/3 0003 17:19
     * @author wangjian
     * @param id 客户ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-删除客户", notes = "企业端-删除客户")
    @DeleteMapping("/deleteCustomer")
    public JsonResult<Integer> deleteCustomer(Integer id){
        //查看客户是否有订单
        WelfareFeeOrder welfareFeeOrder = new WelfareFeeOrder();
        welfareFeeOrder.setCustomersId(id);
        welfareFeeOrder.setStatus(1);
        List<WelfareFeeOrder> select = welfareFeeOrderService.select(welfareFeeOrder);
        if (EmptyUtils.isNotEmpty(select)){
            return JsonResult.failWithMsg("该客户有关联订单，无法删除");
        }
        return JsonResult.success(welfareFeeUserCustomersService.updateCustomersStatus(id));
    }

    /**
     * 删除客户和业务员的关联关系
     * @date 2021/8/4 0004 10:27
     * @author wangjian
     * @param rel 要删除的客户和业务员的关联关系
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-删除客户和业务员的关联关系", notes = "企业端-删除客户和业务员的关联关系")
    @DeleteMapping("/deleteCustomersSalesRel")
    public JsonResult<Integer> deleteCustomersSalesRel(WelfareFeeUserCustomersSalesRel rel){
        if (EmptyUtils.isEmpty(rel.getCustomerId()) || EmptyUtils.isEmpty(rel.getSalesId())) {
            return JsonResult.failWithMsg("请选择客户");
        }
        return JsonResult.success(welfareFeeUserCustomersSalesRelService.delete(rel));
    }

    /**
     * 新增客户和业务员的关联关系
     * @date 2021/8/7 0007 16:55
     * @author wangjian
     * @param id 业务员ID
     * @param customersIds 客户IDS ‘，’隔开
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-新增客户和业务员的关联关系", notes = "企业端-新增客户和业务员的关联关系")
    @PostMapping("/addCustomersSalesRel")
    public JsonResult<Integer> addCustomersSalesRel(Integer id,String customersIds){
        if (EmptyUtils.isEmpty(id) || EmptyUtils.isEmpty(customersIds)) {
            return JsonResult.failWithMsg("请选择客户");
        }
        WelfareFeeUserCustomersSalesRel welfareFeeUserCustomersSalesRel = new WelfareFeeUserCustomersSalesRel();
        welfareFeeUserCustomersSalesRel.setSalesId(id);
        List<WelfareFeeUserCustomersSalesRel> select = welfareFeeUserCustomersSalesRelService.select(welfareFeeUserCustomersSalesRel);
        List<Integer> collect = select.stream().map(WelfareFeeUserCustomersSalesRel::getCustomerId).collect(Collectors.toList());
        String[] split = customersIds.split(",");
        List<WelfareFeeUserCustomersSalesRel> rels = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            if (!collect.contains(split[i])) {
                WelfareFeeUserCustomersSalesRel rel = new WelfareFeeUserCustomersSalesRel();
                rel.setCustomerId(Integer.parseInt(split[i]));
                rel.setSalesId(id);
                rels.add(rel);
            }
        }
        return JsonResult.success(welfareFeeUserCustomersSalesRelService.insertList(rels));
    }
}
